//
//  videoController.swift
//  amicool
//
//  Created by 方君竹 on 2017/6/12.
//  Copyright © 2017年 fjz. All rights reserved.
//

import UIKit

private let reuseIdentifier = "videoCell"

class videoController: UICollectionViewController {
    
    //NSArray,一个数组，保存的是教学资源平台上的视频里的所有项
    var videoList:[AnyObject]!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.navigationBar.barTintColor = UIColor(red: 20/255.0, green: 155/255.0, blue: 1.0, alpha: 1.0)

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        //self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        getVideoList()
        // Do any additional setup after loading the view.
    }
    
    //从远程服务器上获取视频数组
    func getVideoList(){
        let parameters:NSDictionary = ["SessionID":AMNetworkMngTool.loginReturnModel!.sessionid!,
                                       "mod":"video",
                                       "page":"1"]
        //上传一个sessionid，事先从loginreturnjson那个模型里已经存下来了，可以通过它来进行获取，
        AMNetworkMngTool.shared.AMNetwork_GetList(parameters){
            (list) in
            if list != nil {
                DispatchQueue.main.async {
                    self.videoList = [AnyObject]()
                    for video in list!{
                        let model = VideoReturnJson.VideoReturnJsonWithDict(video as? NSDictionary)
                        self.videoList.append(model!)
                    }

                    //self.videoList = videoList
                    self.collectionView?.reloadData()
                }
            }
            else {
            //提示，获取视频列表失败
            }
        }
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // 有多少个session
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // 有多少个单元格由网络返回决定
        if self.videoList != nil {
            return videoList.count
        }
        return 1
    }
    
    //要把原有单元格类型转换为自定义单元格类型
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! VideoCell
        
        //对单元格进行配置
        if self.videoList != nil{
            //从返回得到的videolist中取出模型
            let model = self.videoList[indexPath.row] as! VideoReturnJson
            //赋给对应的属性
            cell.title.text = model.name
            cell.author.text = model.author
            
            
            if model.thumb != nil{
                let imgUrlPre = "http://amicool.neusoft.edu.cn/Uploads/"
                //链接好后封装成一个url
                let imgUrl = URL(string: imgUrlPre + model.thumb!)
                let imgData = try! Data(contentsOf: imgUrl!)
                //通过数据对象生成图片对象
                cell.img.image = UIImage(data: imgData)?.scaleImageToSize(CGSize(width: 80, height: 60))
            }
            else{
                //没有图片的情况下放出来的暂位图
                cell.img.image = UIImage(named: "placeholder.png")
            }
            
        }
        return cell
    }
    
    // MARK: UICollectionViewDelegate
        override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let videoStoryboard = UIStoryboard(name: "Video", bundle: nil)
            let vd = videoStoryboard.instantiateViewController(withIdentifier: "videoDetail") as! VideoDetailController
            //取模型
            vd.videoModle = self.videoList[indexPath.row] as?
            VideoReturnJson
            self.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vd, animated: true)
            self.hidesBottomBarWhenPushed = false
        }

}
